#
# @lc app=leetcode.cn id=200 lang=python3
#
# [200] 岛屿数量
#

# @lc code=start
from typing import List

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        result = 0
        
        for row in range(0, len(grid)):
            for column in range(0, len(grid[row])):
                if grid[row][column] == '1':
                    result += 1
                    self.dfs(row, column, grid)
                    
        
        return result
    
    def dfs(self, row: int, column: int, grid: List[List[str]]):
        # 行列超过地图边界
        if row < 0 or column < 0:
            return 
        if row >= len(grid) or column >= len(grid[row]):
            return
        
        if grid[row][column] == '0':
            return
        
        grid[row][column] = '0'
        # 深度遍历四周的节点
        for r, c in [(row - 1, column), (row + 1, column), (row, column - 1), (row, column + 1)]:
            self.dfs(r, c, grid)
        
        return
# Solution().numIslands([["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]])
# @lc code=end

